package com.ruoyi.query.controller;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.query.entity.Query;
import com.ruoyi.query.service.QueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 动态查询前端控制器
 * </p>
 *
 * @author XiaShiLong
 * @since 2021-03-25
 */
@RestController
@RequestMapping("/query")
@Api("动态查询")
public class QueryController extends BaseController {

    private final QueryService service;

    @Autowired
    public QueryController(QueryService service) {
        this.service = service;
    }

    @PreAuthorize("@ss.hasPermi('query:post:add')")
    @Log(title = "用户添加自定义动态查询", businessType = BusinessType.INSERT)
    @ApiOperation("用户添加自定义动态查询")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Query query) {
        return toAjax(service.save(query));
    }

    @PreAuthorize("@ss.hasPermi('query:post:adminAdd')")
    @Log(title = "管理员添加自定义动态查询", businessType = BusinessType.INSERT)
    @ApiOperation("管理员添加自定义动态查询")
    @PostMapping("/adminAdd")
    public AjaxResult adminAdd(@RequestBody Query query) {
        return toAjax(service.adminSave(query));
    }

    @PreAuthorize("@ss.hasPermi('query:post:adminRemove')")
    @Log(title = "管理员删除自定义查询", businessType = BusinessType.DELETE)
    @ApiOperation("管理员删除自定义查询")
    @DeleteMapping("/adminRemove")
    public AjaxResult adminRemove(@RequestBody Query query) {
        return toAjax(service.adminRemove(query));
    }

    @PreAuthorize("@ss.hasPermi('query:post:adminUpdate')")
    @Log(title = "管理员更新自定义查询", businessType = BusinessType.UPDATE)
    @ApiOperation("管理员更新自定义查询")
    @PutMapping("/adminUpdate")
    public AjaxResult adminUpdate(@RequestBody Query query) {
        return toAjax(service.adminUpdate(query));
    }

    @PreAuthorize("@ss.hasPermi('query:post:pageQuery')")
    @ApiOperation("分页查询")
    @PostMapping("/pageQuery/{page}/{size}")
    public TableDataInfo pageQuery(@PathVariable int page, @PathVariable int size, @RequestBody Query query) {
        startPage(page, size);
        return getDataTable(service.pageQuery(page, size, query));
    }

    @PreAuthorize("@ss.hasPermi('query:post:userRemove')")
    @Log(title = "用户删除自定义查询", businessType = BusinessType.DELETE)
    @ApiOperation("用户删除自定义查询")
    @DeleteMapping("/userRemove")
    public AjaxResult userRemove(@RequestBody Query query) {
        return toAjax(service.userRemove(query));
    }
}
